local tool = {}
-- -- 继承模板 --先刘晓的测试，如果好用，就大家用
function tool.class(classname,super)
    local class_type={}
    class_type.name = classname
    class_type.Ctor=false
    class_type.super=super
    class_type.New=function(type,...)
            local obj={}
            obj.class_type = type;
            obj.super = {};
            if  obj.super and super then
            setmetatable(obj.super,super)
            -- setmetatable(obj.super,{ __index = super})
            end
            class_type.__index = class_type
            setmetatable(obj,class_type)

            -- setmetatable(obj,{__index = class_type})
            do
                local create
                create = function(c,...)
                    if c.super then
                        create(c.super,...)
                    end
                    if c.Ctor then
                        c.Ctor(obj,...)
                    end
                end
                create(type,...)
            end
            return obj
    end

    if super then
        setmetatable(class_type,{__index=
            function(_,k)
                local ret=super[k]
                class_type[k]=ret
                return ret
            end
        })
    end
    return class_type
end


function tool.insertSort(arrayTable, value)
    local len = #arrayTable
    local insertIndex = len + 1
    for i = 1, len do
        if value < arrayTable[i] then
            insertIndex = i
            break
        end
    end
    table.insert(arrayTable, insertIndex, value)
end

function tool.insertSortUnique(arrayTable, value)
    local len = #arrayTable
    local insertIndex = len + 1
    for i = 1, len do
        if value == arrayTable[i] then
            return
        end
        if value < arrayTable[i] then
            insertIndex = i
            break
        end
    end
    table.insert(arrayTable, insertIndex, value)
end

-- function tool.insertSortUnique(arrayTable, value)
--     local len = #arrayTable
--     local target = len + 1

--     for i = len, 1, -1 do
--         if value > arrayTable[i] then
--             while target > i do

--                 if i == len then
--                     table.insert(arrayTable, value)
--                     return
--                 else
--                     arrayTable[target] = arrayTable[target - 1]
--                     target = target - 1
--                 end
--             end
--             arrayTable[i + 1] =  value
--             return
--         end
--     end
--     table.insert(arrayTable, 1, value)
-- end

function tool.insertSortUnique(a, v)
    local low = 1
    local high = #a
    local mid = 1
    while(low <= high) do
        mid = math.floor(( low + high) / 2)
        if a[mid] < v  then
            low = mid + 1
        elseif a[mid] > v then
            high = mid - 1
        else
            break
        end
    end
    mid = math.max(low,mid)
    table.insert(a, mid, v)
end

local rankQueue = tool.class('rankQueue')

function rankQueue:Ctor()
    self.table = {}
end

function rankQueue:pop()
    local t = self:last()
    table.remove(self.table, 1 )
    return t
end

function rankQueue:last()
    return self.table[1]
end

function rankQueue:insert(t)
   tool.insertSortUnique(self.table, t)
end

tool.rankQueue = rankQueue
return tool
